package com.jyw.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jyw.R;
import com.jyw.network.OkHttpManager;
import com.jyw.network.Server;
import com.jyw.pojo.OrderDetail;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author simonw
 * @version 1.0
 * @created 2017/1/6
 */

public class OrderActivity extends BaseActivity implements View.OnClickListener {
    private TextView tvKuai0, tvKuai1, tvName, tvPhone, tvAddress;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        tvKuai0 = (TextView) findViewById(R.id.tv_kuai_0);
        tvKuai1 = (TextView) findViewById(R.id.tv_kuai_1);
        tvName = (TextView) findViewById(R.id.tv_name);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
        tvAddress = (TextView) findViewById(R.id.tv_address);

        findViewById(R.id.iv_back).setOnClickListener(this);

        loadData();
    }

    private void loadData() {
        Request request = new Request.Builder().
                url(Server.GET_ORDER + "?id_data=" + getIntent().getStringExtra("order_id")).
                build();
        OkHttpManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                OkHttpManager.backgroundThreadShortToast(OrderActivity.this, "错误：" + e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String json = response.body().string();
                final OrderDetail orderDetail = new Gson().fromJson(json, OrderDetail.class);
                Log.e("simonwLog", orderDetail.toString());
                new AsyncTask<Void, Void, Void>() {
                    @Override
                    protected Void doInBackground(Void... params) {
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        super.onPostExecute(aVoid);
                        try {
                            JSONObject o = new JSONObject(json);
                            o.getString("address");
                            o.getString("order_id");
                            o.getString("phone");
                            tvKuai0.setText("订单编号： " + orderDetail.getOrder_id()
                                    + "\n取件时间： " + orderDetail.getService_time()
                                    + "\n服务项目： " + o.getString("class"));
                        } catch (JSONException e) {
                            Log.e("simonwLog", "json解析错误:" + e.getMessage());
                        }

                        List<OrderDetail.Re> reList = orderDetail.getRe_arr();
                        String str = "";
                        if (reList != null && reList.size() > 0) {
                            for (OrderDetail.Re re : reList) {
                                str += re.getContent() + "\n" + re.getTime() + "\n";
                            }
                            str.substring(0, str.length() - 2);
                            tvKuai1.setText(str);
                        }
                        if (orderDetail.getAddress_arr() != null) {
                            tvName.setText("姓名：" + orderDetail.getAddress_arr().getName());
                            tvPhone.setText("电话：" + orderDetail.getAddress_arr().getPhone());
                            tvAddress.setText(orderDetail.getAddress_arr().getCity() + " "
                                    + orderDetail.getAddress_arr().getCircle());
                        }

                    }
                }.execute();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
        }
    }
}
